#define __TIMER_C__

#include <gd32f30x.h>
#include "los_interrupt.h"
#include "los_arch_interrupt.h"

void TIM2_IRQHandler();

//1秒产生一次中断 int_time = CLK / ((prescaler+1) *  (period+1))
//CLK = 60MHz
void timer1_init(void)
{
    
    HalHwiCreate(TIMER1_IRQn, 0, 0,TIM2_IRQHandler,NULL);
   
    timer_parameter_struct timer_parameter;

	rcu_periph_clock_enable(RCU_TIMER1);

	//预分频
    timer_parameter.prescaler = 1999,
    //对齐模式
    timer_parameter.alignedmode = TIMER_COUNTER_EDGE,
    //定时器增长方向
    timer_parameter.counterdirection = TIMER_COUNTER_UP,
    //定时器自动加载值
    timer_parameter.period = 59,
    //时钟分频值
    timer_parameter.clockdivision = TIMER_CKDIV_DIV4,

    timer_init(TIMER1, &timer_parameter);
   
    timer_interrupt_enable(TIMER1, TIMER_INT_UP);
    nvic_irq_enable(TIMER1_IRQn, 0, 2);
  
    timer_enable(TIMER1);

    
}
/*
UINT32 HalHwiCreate(HWI_HANDLE_T hwiNum,
                                          HWI_PRIOR_T hwiPrio,
                                          HWI_MODE_T mode,
                                          HWI_PROC_FUNC handler,
                                          HWI_ARG_T arg)
*/
int flg = 0;
void TIM2_IRQHandler()
{
    if (timer_interrupt_flag_get(TIMER1, TIMER_INT_UP) != RESET)
	{
		lv_tick_inc(1);
    	timer_interrupt_flag_clear(TIMER1, TIMER_INT_UP);
    }
}

